package ATM;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录
 */
public class LoginPanel extends JPanel {

    private JLabel accountLabel = new JLabel("账号：");
    private JLabel pwdLabel = new JLabel("密码：");
    private JTextField accountTextField = new JTextField();
    private JPasswordField pwdField = new JPasswordField();
    private JButton endBtn = new JButton(new ImageIcon("src/ATM/image/buttonOff.jpg"));
    private JButton loginBtn = new JButton(new ImageIcon("src/ATM/image/buttonLogin.jpg"));
    private Font font = new Font("宋体", Font.BOLD,25);
    private int content = 3;

    public LoginPanel(ATMFrame atm){

        this.setLayout(null);
        accountLabel.setBounds(300,200,80,50);
        accountLabel.setFont(font);
        accountTextField.setBounds(380,200,250,50);
        accountTextField.setFont(font);
        pwdLabel.setBounds(300,250,80,50);
        pwdLabel.setFont(font);
        pwdField.setBounds(380,250,250,50);
        pwdField.setFont(font);
        loginBtn.setBounds(420,320,140,50);
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = accountTextField.getText();
                String pwd = new String(pwdField.getPassword());
                for (int i = 0; i < atm.getUsers().size(); i++){
                    if (account.equals(atm.getUsers().get(i).getAccount()) &&
                            pwd.equals(atm.getUsers().get(i).getPwd())){
                        atm.setIndex(i);
                        content = 3;
                        atm.showPanel("menu");
                        return;
                    }
                }
                content--;
                if (content == 0){
                    JOptionPane.showMessageDialog(LoginPanel.this,"错误三次，账号冻结");
                    System.exit(0);
                }
                JOptionPane.showMessageDialog(LoginPanel.this,"账号或密码错误，你还有" + content + "次机会。");

            }
        });
        endBtn.setBounds(424,400,140,50);
        endBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        this.add(accountLabel);
        this.add(accountTextField);
        this.add(pwdField);
        this.add(pwdLabel);
        this.add(loginBtn);
        this.add(endBtn);
        this.setOpaque(false);
    }

    public void Refresh(){
        accountTextField.setText("");
        pwdField.setText("");
    }


}
